package lc_3115_质数的最大距离;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.List;

public class Solution {

    private Set<Integer> prime_list(int limit) {
        int[] arr = new int[limit];
        List<Integer> list = new ArrayList<>();
        for (int i = 2; i < limit; i++) {
            if (arr[i] == 0) {
                list.add(i);
            }
            for (int j : list) {
                if (i * j >= limit)
                    break;
                arr[i * j] = 1;
                if (i % j == 0)
                    break;
            }
        }
        return new HashSet<>(list);
    }

    public int maximumPrimeDifference(int[] nums) {
        int l = 0, r = nums.length - 1;
        Set<Integer> primes = this.prime_list(101);
        for (int i = 0; i < nums.length; i++) {
            if (primes.contains(nums[i])) {
                l = i;
                break;
            }
        }
        for (int i = nums.length - 1; i > -1; i--) {
            if (primes.contains(nums[i])) {
                r = i;
                break;
            }
        }
        return r - l;
    }
}